local skill = fk.CreateSkill{
  name = "tea__zhuwo",
  tags = {Skill.Limited}
}
local U = require "packages/utility/utility"
---@type TrigSkelSpec<TrigFunc>
local zhuwo = {
  anim_type = "big",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skill.name) and player == target and player:usedSkillTimes(skill.name, Player.HistoryGame) == 0 then
      local num = #Fk:getAllCardNames("b", true)
      return #player:getTableMark("@$tea__zhuwo_record") >= num
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cids = {}
    for i = 1, 6 do
      table.insert(cids, room.draw_pile[i])
    end
    player:showCards(cids)
    cids = table.filter(cids, function(id)
      return Fk:getCardById(id).type == Card.TypeBasic
    end)
    while #cids > 0 and not player.dead do
      cids = table.filter(cids, function(id)
        return room:getCardArea(id) == Card.DrawPile
      end)
      local suc, dat = room:askToUseActiveSkill(player, {
        skill_name = "tea__zhujing_zhuwo_active",
        prompt = "#tea__zhujing-invoke",
        extra_data = {
          expand_pile = cids
        }
      })
        if suc and dat and #dat.cards == 1 then
          local card = Fk:cloneCard(dat.interaction)
          card:addSubcards(dat.cards)
          card.skillName = skill.name
          local use = {
            from = player,
            tos = dat.targets,
            card = card,
          }
          table.removeOne(cids, dat.cards[1])
          room:useCard(use)
        else
          break
        end
    end
  end,
}
---@type TrigSkelSpec<TrigFunc>
local zhuwo_re = {
  can_refresh = function(self, event, target, player, data)
    if not player:hasSkill(skill.name, true) then return end
    local cids = {}
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId).type == Card.TypeBasic then
            table.insertIfNeed(cids, info.cardId)
          end
        end
      end
    end
    if #cids > 0 then
      event:setCostData(self, cids)
      return true
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self)
    if player.dead then return end
    local mark = player:getTableMark("@$tea__zhuwo_record")
    for _, id in ipairs(cards) do
      if table.every(mark, function(c)
        return Fk:getCardById(c).trueName ~= Fk:getCardById(id).trueName
      end) then
        table.insert(mark, id)
      end
    end
    room:setPlayerMark(player, "@$tea__zhuwo_record", mark)
  end,
}
skill:addEffect(fk.TurnEnd, zhuwo)
skill:addEffect(fk.AfterCardsMove, zhuwo_re)
return skill